/**
 * Created by fanghonghui on 2017/7/17.
 */
var express = require('express');
var router = express.Router();
var User = require('../models/User');
var Category = require('../models/Category');
var Content= require('../models/Content');
router.use(function(req,res,next){
    if(!req.userInfo.isAdmin){
        res.send('对不起，只有管理员才可以进入后台管理');
        return;
    };
    next();
})
router.get('/',function(req,res){
    res.render('admin/index',{
        userInfo:req.userInfo
    })
});
router.get('/user',function(req,res){
    /*
    从数据库中读取用户信息
    limit(number):限制获取的数据条数
    skip(2):忽略数据的条数
    每页显示2条
    1:1-2 skip:0,
    2:3-4 skip:2
    * */
    var page =req.query.page||1;
    var pages = 0;
    var limit =5;

    User.count().then(function(count){
        pages = Math.ceil(count/limit);
        page = Math.min(page,pages);
        page = Math.max(page,1);
        page = Number(page);
        var skip = (page-1)*limit;
        User.find().limit(limit).skip(skip).then(function(users){
            res.render('admin/user_index',{
                userInfo:req.userInfo,
                users:users,
                page:page,
                type:'user',
                limit:limit,
                count:count,
                pages:pages
            })
        });
    })


});
//分类首页
router.get('/category',function(req,res){
    var page =req.query.page||1;
    var pages = 0;
    var limit =2;

    Category.count().then(function(count){
        pages = Math.ceil(count/limit);
        page = Math.min(page,pages);
        page = Math.max(page,1);
        page = Number(page);
        var skip = (page-1)*limit;
        Category.find().sort({_id:-1}).limit(limit).skip(skip).then(function(categories){
            res.render('admin/category_index',{
                userInfo:req.userInfo,
                categories:categories,
                page:page,
                type:'category',
                limit:limit,
                count:count,
                pages:pages
            })
        });
    })
})
//分类的添加
router.get('/category/add',function(req,res){
    res.render('admin/category_add',{
        userInfo:req.userInfo
    })
})
//分类的保存
router.post('/category/add',function(req,res){
    var name=req.body.name||'';
    if(name==''){
        res.render('admin/error',{
            userInfo:req.userInfo,
            message:'名称不能为空',
            url:''
        })
    } else{
        //数据库中是否已经存在同类分类名称
        Category.findOne({
            name:name
        }).then(function (rs) {
            if(rs){
                res.render('admin/error',{
                    userInfo:req.userInfo,
                    message:'分类已经存在了'
                })
                return Promise.reject();
            } else {
                return new Category({
                    name:name
                }).save();
            }
        }).then(function(newCategory){
            res.render('admin/success',{
                userInfo:req.userInfo,
                message:'保存分类成功',
                url:'/admin/category'
            })
        })
    }

});
//分类修改
router.get('/category/edit',function(req,res){
    var id = req.query.id||'';

    Category.findOne({
        _id:id
    }).then(function(category){

        if(!category){
            res.render('admin/error',{
                userInfo:req.userInfo,
                message:'分类信息不存在'
            })
            return Promise.reject();
        } else {
            res.render('admin/category_edit',{
                userInfo:req.userInfo,
                category:category
            })
        }
    })
})
//分类的添加
router.get('/category/add',function(req,res){
    res.render('admin/category_add',{
        userInfo:req.userInfo
    })
})
//分类的修改保存
router.post('/category/edit',function(req,res){
    var id = req.query.id||'';
    var name=req.body.name||'';
    if(name==''){
        res.render('admin/error',{
            userInfo:req.userInfo,
            message:'名称不能为空',
            url:''
        })
    } else{
        //数据库中是否已经存在同类分类名称
        Category.findOne({
            _id:id
        }).then(function (category) {
            if(!category){
                res.render('admin/error',{
                    userInfo:req.userInfo,
                    message:'分类已经存在了'
                })
                return Promise.reject();
            } else {
               if(name==category.name){
                   res.render('admin/success',{
                       userInfo:req.userInfo,
                       message:'修改成功',
                       url:'/admin/category'
                   })
                   return Promise.reject();
               }else {
                  return Category.findOne({
                       _id:{$ne:id},//id 不等于当前的ID
                       name:name
                   })
               }
            }
        }).then(function(sameCategory){
            if(sameCategory){
                res.render('admin/error',{
                    userInfo:req.userInfo,
                    message:'数据库中存在同名分类',
                    url:'/admin/category'
                })
                return Promise.reject();
            } else {
              return  Category.update({
                    _id:id
                },{
                    name:name
                })
            }

        }).then(function () {
            res.render('admin/success',{
                userInfo:req.userInfo,
                message:'修改成功',
                url:'/admin/category'
            })
        })
    }

});

//分类的删除
router.get('/category/delete',function(req,res){
    var id = req.query.id||'';

    Category.remove({
        _id:id
    }).then(function () {
        res.render('admin/success',{
            userInfo:req.userInfo,
            message:'删除成功',
            url:'/admin/category'
        })
    })


});

//内容首页
router.get('/content',function (req,res) {
    var page =req.query.page||1;
    var pages = 0;
    var limit =3;

    Content.count().then(function(count){
        pages = Math.ceil(count/limit);
        page = Math.min(page,pages);
        page = Math.max(page,1);
        page = Number(page);
        var skip = (page-1)*limit;
        Content.find().limit(limit).skip(skip).populate(['category','user']).then(function(content){
            res.render('admin/content_index',{
                userInfo:req.userInfo,
                content:content,
                page:page,
                type:'content',
                limit:limit,
                count:count,
                pages:pages
            })
        });
    })

});
//内容添加页面

router.get('/content/add',function (req,res) {
    Category.find().sort({_id:-1}).then(function(categories){
        res.render('admin/content_add',{
            userInfo:req.userInfo,
            categories:categories
        })
    });

})
//内容保存
router.post('/content/add',function (req,res) {
    var category = req.body.category;
    var  title = req.body.title;
    if(title==''){
        res.render('admin/error',{
            userInfo:req.userInfo,
            message:'标题不能为空'
        })
        return Promise.reject();
    }
    new Content({
        category:category,
        title:title,
        user:req.userInfo._id.toString(),
        description:req.body.description,
        content:req.body.content
    }).save().then(function(rs){
        res.render('admin/success',{
            userInfo:req.userInfo,
            message:'内容保存成功',
            url:'/admin/content'
        })
    })

});

//修改内容
router.get('/content/edit',function (req,res) {
    var id = req.query.id||'';
    var categories =[];
    Category.find().sort({_id:-1}).then(function(rs){
        categories = rs;
        return  Content.findOne({
            _id:id
        }).populate('category');
    }).then(function(content){
        if(!content){
            res.render('admin/error',{
                userInfo:req.userInfo,
                message:'指定内容不存在'
            })
            return Promise.reject();
        } else {
            res.render('admin/content_edit',{
                userInfo:req.userInfo,
                content:content,
                categories:categories
            })
        }
    })


});

//保存修改内容
router.post('/content/edit',function(req,res){
    var id = req.query.id||'';
    var category = req.body.category;
    var  title = req.body.title;
    if(title==''){
        res.render('admin/error',{
            userInfo:req.userInfo,
            message:'标题不能为空'
        })
        return Promise.reject();
    }
    Content.update({
        _id:id
    },{
        category:category,
        title:title,
        description:req.body.description,
        content:req.body.content
    }).then(function () {
        res.render('admin/success',{
            userInfo:req.userInfo,
            message:'内容修改成功',
            url:'/admin/content'
        })
    })
})
//删除内容
router.get('/content/delete',function (req,res) {
    var id = req.query.id||'';
    Content.remove({
        _id:id
    }).then(function () {
        res.render('admin/success',{
            userInfo:req.userInfo,
            message:'删除成功',
            url:'/admin/content'
        })
    })
});
module.exports = router;